
package inc.gome.dynamic.datasource.logread;


import java.io.*;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import org.apache.commons.io.output.CountingOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.Charset;
import com.jcraft.jzlib.GZIPInputStream;
import org.apache.commons.io.output.WriterOutputStream;

/**
 * @author 苏继宙
 * @Date   2021-12-01
 * @Desc   本类主要完成对日志文件的读取功能
 */
public class LargeText {


    /**
     * 内部类：抽象日志文件资源
     */
    private interface Source {
        Session open() throws IOException;
        long length();
        boolean exists();
    }

    //文件资源
    private final Source source;

    //文件字符编码
    protected final Charset charset;

    //文件完成标记
    private volatile boolean completed;


    //构造方法
    public LargeText(File file, boolean completed) {
        this(file,Charset.defaultCharset(),completed);
    }

    public LargeText(File file, boolean completed, boolean transparentGunzip) {
        this(file, Charset.defaultCharset(), completed, transparentGunzip);
    }

    public LargeText(final File file, Charset charset, boolean completed) {
        //默认不进行压缩文件
        this(file, charset, completed, false);
    }


    public LargeText(final File file, Charset charset, boolean completed, boolean transparentGunzip) {
        this.charset = charset;

        //处理zip文件
        if (transparentGunzip && GzipAwareSession.isGzipStream(file)) {
            this.source = new Source() {
                public Session open() throws IOException {
                    return new GzipAwareSession(file);
                }
    
                public long length() {
                    return GzipAwareSession.getGzipStreamSize(file);
                }
    
                public boolean exists() {
                    return file.exists();
                }
            };
        } else {
            //普通日志文件
            this.source = new Source() {
                public Session open() throws IOException {
                    return new FileSession(file);
                }
    
                public long length() {
                    return file.length();
                }
    
                public boolean exists() {
                    return file.exists();
                }
            };
        }
        this.completed = completed;
    }

    //缓存读取
    public LargeText(ByteBuffer memory, boolean completed) {
        this(memory,Charset.defaultCharset(),completed);
    }

    public LargeText(final ByteBuffer memory, Charset charset, boolean completed) {
        this.charset = charset;
        //缓存文件资源
        this.source = new Source() {
            public Session open() throws IOException {
                return new BufferSession(memory);
            }

            public long length() {
                return memory.length();
            }

            public boolean exists() {
                return true;
            }
        };
        this.completed = completed;
    }

    //标记完成
    public void markAsComplete() {
        completed = true;
    }

    //判断是否完成
    public boolean isComplete() {
        return completed;
    }

    //文件长度
    public long length() {
        return source.length();
    }



    //全部读取
    public Reader readAll() throws IOException {
        return new InputStreamReader(new InputStream() {
            final Session session = source.open();

            public int read() throws IOException {
                byte[] buf = new byte[1];
                int n = session.read(buf);
                if(n==1)    return buf[0];
                else        return -1; // EOF
            }

            public int read(byte[] buf, int off, int len) throws IOException {
                return session.read(buf,off,len);
            }

            public void close() throws IOException {
                session.close();
            }

        },charset);

    }

    //写日志
    public long writeLogTo(long start, Writer w) throws IOException {
        return writeLogTo(start, new WriterOutputStream(w, charset));
    }


    /**
     *
     * @param start  从哪里开始写
     * @param out  输出到哪里
     * @return
     * @throws IOException
     */
    public long writeLogTo(long start, OutputStream out) throws IOException {
        CountingOutputStream os = new CountingOutputStream(out);

        try (Session f = source.open()) {
            //移动光标
            f.skip(start);
            //判断是否完整读取
            if (completed) {
                // write everything till EOF
                byte[] buf = new byte[1024];
                int sz;
                //直接读出去
                while ((sz = f.read(buf)) >= 0)
                    os.write(buf, 0, sz);
            } else {
                //固定大小读取一次
                ByteBuf buf = new ByteBuf(null, f);
                HeadMark head = new HeadMark(buf);
                TailMark tail = new TailMark(buf);
                buf = null;  //释放
                //这里的 head和 tail 主要是用来行成链表 回收缓存区块(重复使用) 利用头尾结点移动实现文件的读取
                int readLines = 0;
                while (tail.moveToNextLine(f) && readLines++ < MAX_LINES_READ) {  //移动到下一行  13
                    head.moveTo(tail, os);
                }
                head.finish(os);
            }
        }
        os.flush();
        return os.getByteCount()+start;
    }

    /**
     * Implements the progressive text handling.
     * This method is used as a "web method" with progressiveText.jelly.
     */
    public void doProgressText(HttpServletRequest req, HttpServletResponse rsp) throws IOException {
        setContentType(rsp,req);
        rsp.setStatus(HttpServletResponse.SC_OK);

        //判断文件是否存在
        if(!source.exists()) {
            // file doesn't exist yet
            rsp.addHeader("X-Text-Size","0");
            rsp.addHeader("X-More-Data","true");
            return;
        }

        long start = 0;
        String s = req.getParameter("start");
        if(s!=null)
            start = Long.parseLong(s);
        //保底 若文件长度小于start  将start置为0
        if(source.length() < start )
            start = 0;  // text rolled over
        //初始化一个字符线轴  相当于一个 输出流
        CharSpool spool = new CharSpool();
        long r = writeLogTo(start,spool);

        rsp.addHeader("X-Text-Size",String.valueOf(r));
        if(!completed)
            rsp.addHeader("X-More-Data","true");

        Writer w = createWriter(req, rsp, r - start);

        //换行符写出
        spool.writeTo(new LineEndNormalizingWriter(w));
        w.close();
    }

    protected void setContentType(HttpServletResponse rsp,HttpServletRequest req) {
        rsp.setContentType("text/plain;charset=UTF-8");
    }

    protected Writer createWriter(HttpServletRequest req, HttpServletResponse rsp, long size) throws IOException {
        // when sending big text, try compression. don't bother if it's small
        if(size >4096)
            return getCompressedWriter(req,rsp);
        else
            return rsp.getWriter();
    }

    public Writer getCompressedWriter(HttpServletRequest req,HttpServletResponse rsp) throws IOException {
            return rsp.getWriter();
    }
    /**
     * 记录当前读取光标在ByteBuf中的位置
     */
    private static class Mark {
        protected ByteBuf buf;
        protected int pos;

        public Mark(ByteBuf buf) {
            this.buf = buf;
        }
    }

    /**
     * head  Mark
     */
    private static final class HeadMark extends Mark {
        public HeadMark(ByteBuf buf) {
            super(buf);
        }

        void moveTo(Mark that, OutputStream os) throws IOException {
            //以上while() 主要处理   头结点移动判断 当头结点和尾结点不是一个 buf的时候 标识单签buff已经标记完成 把当前buff写出
            // 头结点写出 并往后移动一个buf
            while(this.buf!=that.buf) {
                //写出
                os.write(buf.buf,0,buf.size);
                //头移动
                buf = buf.next;
                pos = 0;
            }
            //记录头结点缓存区和尾结点缓存区  pos一致    当位于最后一行且没有换行的时候(是读取不到最后)
            this.pos = that.pos;
        }
        //没有换行 /r/n 另起一行
        void finish(OutputStream os) throws IOException {
            os.write(buf.buf,0,pos);
        }
    }

    /**
     * 尾巴节点  当链表满之后 尾巴节点会自动生成一个新节点 并完成读取
     */
    private static final class TailMark extends Mark {
        public TailMark(ByteBuf buf) {
            super(buf);
        }
        //移动到下一行
        boolean moveToNextLine(Session f) throws IOException {
            while(true) {
                while(pos==buf.size) {
                    if(!buf.isFull()) {
                        return false;   //未写满  读取完成 跳出
                    } else {
                        //buf 重复使用空间
                        buf = new ByteBuf(buf,f); //形成链表
                        pos = 0;
                    }
                }
                byte b = buf.buf[pos++];
                if(b=='\r' || b=='\n')
                    return true;
            }
        }
    }

    /**
     * 一次允许读取的缓存字节
     */
    private static final class ByteBuf {
        private final byte[] buf = new byte[1024];   //大小取值参考  默认1024
        private int size = 0;

        //链表读取
        private ByteBuf next;

        public ByteBuf(ByteBuf previous, Session f) throws IOException {
            if(previous!=null) {   //存在前置节点
                assert previous.next==null;  //判断  previous.next 是否==null
                previous.next = this;
            }
            //继续读取
            while(!this.isFull()) {
                int chunk = f.read(buf, size, buf.length - size);
                if(chunk==-1)
                    return;
                size+= chunk;
            }
        }

        public boolean isFull() {
            return buf.length==size;
        }
    }


    /**
     * 一次文件的读取封装尾一次会话
     */
    private interface Session extends Closeable {
        void skip(long start) throws IOException;
        int read(byte[] buf) throws IOException;
        int read(byte[] buf, int offset, int length) throws IOException;
    }



    /**
     * FileSession   文件读取会话实现
     * 1. 使用RandomAccessFile  大型文本日志类文件的快速定位获取数据
     *
     */
    private static final class FileSession implements Session {
        private final RandomAccessFile file;

        public FileSession(File file) throws IOException {
            //赋予可读权限
            this.file = new RandomAccessFile(file,"r");
        }

        @Override
        public void close() throws IOException {
            file.close();
        }


        @Override
        public void skip(long start) throws IOException {
            //移动偏移量
            /**
             * seek：
             * 指定文件的光标位置，通俗点说就是指定你的光标位置，然后下次读文件数据的时候从该位置读取。
             * getFilePointer()：
             * 返回当前的文件光标位置
             */
            file.seek(file.getFilePointer()+start);
        }

        @Override
        public int read(byte[] buf) throws IOException {
            return file.read(buf);
        }

        @Override
        public int read(byte[] buf, int offset, int length) throws IOException {
            return file.read(buf,offset,length);
        }
    }
    
    /**
     *  GZIP文件读取封装  此次开发不做扩展
     */
    private static final class GzipAwareSession implements Session {
        private final GZIPInputStream gz;

        public GzipAwareSession(File file) throws IOException {
            this.gz = new GZIPInputStream(Files.newInputStream(file.toPath(), StandardOpenOption.READ));
        }

        @Override
        public void close() throws IOException {
            gz.close();
        }

        @Override
        public void skip(long start) throws IOException {
            while (start > 0) {
                start -= gz.skip(start);
            }
        }

        @Override
        public int read(byte[] buf) throws IOException {
            return gz.read(buf);
        }

        @Override
        public int read(byte[] buf, int offset, int length) throws IOException {
            return gz.read(buf,offset,length);
        }
    
        /**
         * Checks the first two bytes of the target file and return true if
         * they equal the GZIP magic number.
         * @param file
         * @return true, if the first two bytes are the GZIP magic number.
         */
        public static boolean isGzipStream(File file) {
            try (InputStream in = Files.newInputStream(file.toPath(), StandardOpenOption.READ);
                 DataInputStream din = new DataInputStream(in)) {
                return din.readShort()==0x1F8B;
            } catch (IOException ex) {
                return false;
            }
        }
        
        /**
         * Returns the uncompressed size of the file in a quick, but unreliable
         * manner. It will not report the correct size if:
         * <ol>
         * <li>The compressed size is larger than 2<sup>32</sup> bytes.</li>
         * <li>The file is broken or truncated.</li>
         * <li>The file has not been generated by a standard-conformant compressor.</li>
         * <li>It is a multi-volume GZIP stream.</li>
         * </ol>
         * <p>
         * The advantage of this approach is, that it only reads the first 2
         * and last 4 bytes of the target file. If the first 2 bytes are not
         * the GZIP magic number, the raw length of the file is returned.
         * 
         * @see #isGzipStream(File)
         * @param file
         * @return the size of the uncompressed file content.
         */
        public static long getGzipStreamSize(File file) {
            if (!isGzipStream(file)) {
                return file.length();
            }
            RandomAccessFile raf = null;
            try {
                raf = new RandomAccessFile(file, "r");
                if (raf.length() <= 4) {
                    raf.close();
                    return file.length();
                }
                raf.seek(raf.length() - 4);
                int b4 = raf.read();
                int b3 = raf.read();
                int b2 = raf.read();
                int b1 = raf.read();
                return (b1 << 24) + (b2 << 16) + (b3 << 8) + b4;
            } catch (IOException ex) {
                return file.length();
            } finally {
                if (raf!=null)
                    try {
                        raf.close();
                    } catch (IOException e) {
                        // ignore
                    }
            }
        }
    }


    /**
     * BufferSession  缓存读取实现
     */
    private static final class BufferSession implements Session {
        private final InputStream in;

        public BufferSession(ByteBuffer buf) {
            this.in = buf.newInputStream();
        }

        @Override
        public void close() throws IOException {
            in.close();
        }

        @Override
        public void skip(long start) throws IOException {
            while (start > 0) {
                long diff = in.skip(start);
                if (diff == 0) {
                    throw new EOFException("Attempting to read past end of buffer");
                }
                start -= diff;
            }
        }

        @Override
        public int read(byte[] buf) throws IOException {
            return in.read(buf);
        }

        @Override
        public int read(byte[] buf, int offset, int length) throws IOException {
            return in.read(buf,offset,length);
        }
    }

    /**
     * 最大允许读取行
     */
    private static final int MAX_LINES_READ = 10000;

}
